/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.commodity.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.RedisUtil;
import org.springblade.modules.commodity.entity.CommodityParams;
import org.springblade.modules.commodity.entity.CommodityType;
import org.springblade.modules.commodity.service.ICommodityParamsService;
import org.springblade.modules.system.entity.Dict;
import org.springblade.modules.system.entity.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.commodity.entity.Commodity;
import org.springblade.modules.commodity.vo.CommodityVO;
import org.springblade.modules.commodity.service.ICommodityService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 商品表 控制器
 *
 * @author Blade
 * @since 2021-01-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/pxx_commodity/commodity")
@Api(value = "商品表", tags = "商品表接口")
public class CommodityController extends BladeController {

    private ICommodityService commodityService;
    private ICommodityParamsService commodityParamsService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入commodity")
    public R<Commodity> detail(Commodity commodity) {
        Commodity detail = commodityService.getOne(Condition.getQueryWrapper(commodity));
        CommodityParams params = commodityParamsService.getOne(Wrappers.<CommodityParams>query()
                .lambda().eq(CommodityParams::getCommodityId,detail.getId()));
        CommodityVO commodityVO = BeanUtil.copy(detail, CommodityVO.class);
        commodityVO.setCommodityParams(params);
        return R.data(commodityVO);
    }

    /**
     * 分页 商品表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入commodity")
    public R<IPage<Commodity>> list(Commodity commodity, Query query) {
        IPage<Commodity> pages = commodityService.page(Condition.getPage(query), Condition.getQueryWrapper(commodity));
        return R.data(pages);
    }

    /**
     * 自定义分页 商品表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入commodity")
    public R<IPage<CommodityVO>> page(CommodityVO commodity, Query query) {
        IPage<CommodityVO> pages = commodityService.selectCommodityPage(Condition.getPage(query), commodity);
        return R.data(pages);
    }

    /**
     * 新增 商品表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入commodity")
    public R save(@Valid @RequestBody Commodity commodity) {
        return R.status(commodityService.save(commodity));
    }

    /**
     * 修改 商品表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入commodity")
    public R update(@Valid @RequestBody Commodity commodity) {
        return R.status(commodityService.updateById(commodity));
    }

    /**
     * 新增或修改 商品表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入commodity")
    public R submit(@Valid @RequestBody Commodity commodity) {
        return R.status(commodityService.saveOrUpdate(commodity));
    }

    /**
     * 新增或修改 商品表
     */
    @PostMapping("/submits")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入commodity")
    public R submits(@Valid @RequestBody Commodity commodity) {
        return R.status(commodityService.submits(commodity));
    }


    /**
     * 删除 商品表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(commodityService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 加入我的历史
     */
    @PostMapping("/history")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "添加我的历史", notes = "")
    public R history(@RequestBody Commodity commodity) {
//		Boolean history = commodityService.history(commodity);
//		return R.status(history);
        return R.data(true);
    }

    /**
     * 查看我的历史
     */
    @GetMapping("/selectHistory")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "查看我的历史", notes = "")
    public R selectHistory() {
//		List<Commodity> commodityList = commodityService.selectHistory();
//		return R.data(commodityList);
        return R.data(true);
    }

    @PostMapping("/upload")
    @ApiOperationSupport(order = 19)
    @ApiOperation(value = "上传图片", notes = "")
    public R upload(MultipartFile file) throws IOException {
        Map<String, String> map = new HashMap<>();
        String filename = UUID.randomUUID().toString();
        //String path = "D:\\zzz\\saber\\public\\img";
        String path = "E:\\project\\Saber\\public\\img";
        String name = ".jpg";
        File newFile = new File(path + "/" + filename + name);
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        file.transferTo(newFile);
        String url = "img/" + filename + name;
        String imgName = filename + name;
        map.put("name", imgName);
        map.put("url", url);
        return R.data(map);
    }

    /**
     * 自定义分页 商品表
     */
    @GetMapping("/commodityPage")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入commodity")
    public R<IPage<CommodityVO>> commodityPage(CommodityVO commodity, Query query) {
        IPage<CommodityVO> pages = commodityService.selectPage(Condition.getPage(query), commodity);
        return R.data(pages);
    }

    /**
     * 商品类型字典
     */
    @GetMapping("/commodityType")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入commodity")
    public R commodityType() {
        List<Dict> dictList = commodityService.selectType();
        return R.data(dictList);
    }

    /**
     * 详情2
     */
    @GetMapping("/getCommodityDetail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入commodity")
    public R<Commodity> getCommodityDetail(Commodity commodity) {
        CommodityVO commodityVO = commodityService.selectId(commodity.getId());
        return R.data(commodityVO);
    }

    @PostMapping("/uploadAvue")
    @ApiOperationSupport(order = 19)
    @ApiOperation(value = "前台Avue上传上传图片", notes = "")
    public R uploadAvue(MultipartFile file) throws IOException {
        Map<String, String> map = new HashMap<>();
        String filename = UUID.randomUUID().toString();
        //String path = "D:\\zzz\\saber\\public\\img";
        String path = "E:\\project\\Saber\\public\\img";
        String name = ".jpg";
        File newFile = new File(path + "/" + filename + name);
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        file.transferTo(newFile);
        String url = "img/" + filename + name;
        String imgName = filename + name;
        map.put("name", imgName);
        map.put("url", "http://localhost:7777/"+url);
        return R.data(map);
    }
}
